package com.passgen;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: pvg
 * Date: 10/22/11
 * Time: 9:08 PM
 */
public class PasswordGeneratorForm {
    private JComboBox myPassLenght;
    private JComboBox myFiltersSet;
    private JButton generate;
    private JButton copyButton;
    private JPanel passGenMainPanel;
    private JLabel passwordLabel;
  private JFrame frame;


  public PasswordGeneratorForm(final JFrame frame) {
    generate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                Object passLenghtSelectedItem = myPassLenght.getSelectedItem();

                PasswordGeneratorUtils.CharList filtersSetSelectedItem = (PasswordGeneratorUtils.CharList) myFiltersSet.getSelectedItem();
                int passLength = 8;
                try {
                   passLength = Integer.parseInt(passLenghtSelectedItem.toString());
                } catch (Exception ignore) {
                }
                String passwordResult = PasswordGeneratorUtils.randomString(passLength, filtersSetSelectedItem.getValue());
                passwordLabel.setText(passwordResult);
                frame.pack();
            }
        });
    }

    private void createUIComponents() {
        myFiltersSet = new JComboBox(PasswordGeneratorUtils.CharList.values());
        myFiltersSet.setSelectedItem(PasswordGeneratorUtils.CharList.DIGITS_LETTERS);
    }

    public JComponent getMainPanel() {
        return passGenMainPanel;
    }

    public JButton getGenerate() {
        return generate;
    }
}
